<?php  

/** 
* Requests page (Controller) 
* Страничка заявок (контроллер) 
* @author IT studio IRBIS-team 
* @copyright © 2009 IRBIS-team 
*/
/////////////////////////////////////////////////////////

/**
* Generation of page of an error at access out of system
* Генерация страницы ошибки при доступе вне системы
*/
    if(!defined('IRB_KEY'))
    {
       header("HTTP/1.1 404 Not Found");      
       exit(file_get_contents('../../404.html'));
    } 
//////////////////////////////////////////////////////////

/**
* We connect a file of the MySQL functions
* Подключаем файл функций MySQL
*/  
	include './libs/mysql.php';
    
	$user['id'] = (int)$_SESSION['user_data']['id'];
	
	$user_domains = getDomsOfUser($user['id']);
	
	foreach($user_domains as $ud_key => $ud)
	{
		
		$query_t = mysqlQuery("SELECT * FROM `". IRB_DBPREFIX ."requests_types`");
		
		while($row_t = mysql_fetch_assoc($query_t))
			{
			$requests[$row_t['id']] = $row_t;
			$query = mysqlQuery("SELECT DISTINCT r.id as r_id,
									r.title as r_title,
									r.text as r_text,
									r.target_date as r_date,
									r.type as r_type,
									d.id as d_id,
									d.caption as d_caption,
									u.id as u_id,										
									u.name as u_name,
									u.surname as u_surname,
									u.phone as u_phone,
									u.photo as u_photo										
							FROM 	
									`". IRB_DBPREFIX ."requests` r,
									`". IRB_DBPREFIX ."user` u,
									`". IRB_DBPREFIX ."domains` d,
									`". IRB_DBPREFIX ."requests_link_dom_user` rldu,
									`". IRB_DBPREFIX ."dom_link_user` dlu
							WHERE	dlu.user_id = '".$user['id']."'
							AND		dlu.domain_id = '".$ud['d_id']."'
							AND		dlu.domain_id = rldu.dom_id
							AND		u.id = r.owner
							AND		d.id = 	rldu.dom_id
							AND		r.id = rldu.request_id
							AND		r.closed=0
							AND 	r.type='".$row_t['id']."'");
							
			
			while($row_r = mysql_fetch_assoc($query))
				{
				if(empty($row_r['photo']))
					$row_r['u_photo'] = 'http://socdomen.ru/skins/images/no-photo-small.gif';
				$requests[$row_t['id']][] = $row_r;
				}
			}

			
			$dom_requests[$ud['d_id']] = $requests;
			$dom_requests[$ud['d_id']]['caption'] = $ud['d_caption'];
	}
// История событий
	$query_h = mysqlQuery("SELECT r.*,u.name,u.surname 
							FROM `". IRB_DBPREFIX ."requests` r,
								`". IRB_DBPREFIX ."user` u
							WHERE r.closed=1
							AND u.id = r.owner
							ORDER BY r.modified_date DESC LIMIT 5");
	if(mysql_num_rows($query_h))
		{
		while($row_h = mysql_fetch_assoc($query_h))
			{
			$r_h[] = $row_h;
			}
		}
